package com.supermarket.product.controller;


import com.supermarket.common.core.utils.poi.ExcelUtil;
import com.supermarket.common.core.web.controller.BaseController;
import com.supermarket.common.core.web.domain.AjaxResult;
import com.supermarket.common.core.web.page.TableDataInfo;
import com.supermarket.common.log.annotation.Log;
import com.supermarket.common.log.enums.BusinessType;
import com.supermarket.common.security.annotation.RequiresPermissions;
import com.supermarket.product.domain.ProductCategories;
import com.supermarket.product.domain.ProductGoods;
import com.supermarket.product.service.IProductCategoriesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品分类控制层
 *
 * @author
 * @date
 */
@RestController
@RequestMapping("/categories")
@Tag(name = "商品分类")
public class ProductCategoriesController extends BaseController
{
    @Autowired
    private IProductCategoriesService productCategoriesService;

    /**
     * 查询商品分类列表
     */
    @RequiresPermissions("product:categories:list")
    @GetMapping("/list")
    @Operation(summary = "查询商品分类列表")
    public TableDataInfo list(ProductCategories productCategories)
    {
        startPage();
        List<ProductCategories> list = productCategoriesService.selectProductCategoriesList(productCategories);
        return getDataTable(list);
    }

    /**
     * 导出商品分类列表
     */
    @RequiresPermissions("product:categories:export")
    @Log(title = "商品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "导出商品分类列表")
    public void export(HttpServletResponse response, ProductCategories productCategories)
    {
        List<ProductCategories> list = productCategoriesService.selectProductCategoriesList(productCategories);
        ExcelUtil<ProductCategories> util = new ExcelUtil<ProductCategories>(ProductCategories.class);
        util.exportExcel(response, list, "商品分类数据");
    }

    /**
     * 获取商品分类详细信息
     */
    @RequiresPermissions("product:categories:query")
    @GetMapping(value = "/{id}")
    @Operation(summary = "获取商品分类详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(productCategoriesService.selectProductCategoriesById(id));
    }

    /**
     * 新增商品分类
     */
    @RequiresPermissions("product:categories:add")
    @Log(title = "商品分类", businessType = BusinessType.INSERT)
    @PostMapping
    @Operation(summary = "新增商品分类")
    public AjaxResult add(@RequestBody ProductCategories productCategories)
    {
        productCategoriesService.insertProductCategories(productCategories);
        return AjaxResult.success("添加成功");
    }

    /**
     * 修改商品分类
     */
    @RequiresPermissions("product:categories:edit")
    @Log(title = "商品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    @Operation(summary = "修改商品分类")
    public AjaxResult edit(@RequestBody ProductCategories productCategories)
    {
        return toAjax(productCategoriesService.updateProductCategories(productCategories));
    }

    /**
     * 删除商品分类
     */
    @RequiresPermissions("product:categories:remove")
    @Log(title = "商品分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除商品分类")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(productCategoriesService.deleteProductCategoriesByIds(ids));
    }

    /**
     * 商品分类
     * @param
     * @return
     */
    @GetMapping("getList")
    @Operation(summary = "商品分类两级联动")
     public AjaxResult getList() {
       List<ProductCategories> list=  productCategoriesService.getList();
        return success(list);
    }

    /**
     * 获取前端分类页面所需的所有初始数据
     * (包括分类树和默认的商品列表)
     */
    @GetMapping("/page-init")
    public AjaxResult getPageInitData() {
        return AjaxResult.success(productCategoriesService.getCategoryTreeWithInitialProducts());
    }

    /**
     * 根据二级分类ID查询商品列表
     */
    @Operation(summary = "根据二级分类ID查询商品列表")
    @GetMapping("/listByCategory/{categoryId}")
    public TableDataInfo listByCategory(@PathVariable("categoryId") Integer categoryId)
    {
        startPage();
        List<ProductGoods> list = productCategoriesService.selectProductGoodsListByCategoryId(categoryId);
        return getDataTable(list);
    }
    /**
     * 获取商品分类递归组装树
     * @return
     */
    @GetMapping("/categoryOptionsListAll")
    public AjaxResult categoryOptionsListAll() {
        return AjaxResult.success(productCategoriesService.getCategoryTree());
    }

}
